package com.joesem.sale.client.widget.dialog

import android.app.Application
import android.os.Bundle
import androidx.fragment.app.FragmentManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.jiaxing.sale.client.R
import com.jiaxing.sale.client.databinding.DialogWorkOderParamsBinding
import com.joesem.sale.client.base.*
import com.joesem.sale.client.network.data.WorkOrderParamsMode
import com.joesem.sale.client.utils.click

const val WO_TYPES = "wo_types"
const val LEVELS = "levels"
const val STATUS = "status"
const val TYPES = "types"
const val CONTENTS = "contents"
const val PROJECT_NAMES = "project_names"
const val PROJECT_UNITS = "project_units"
const val RECEIPT_REASON = "receipt_reason"
const val RECEIPT_TYPE = "receipt_type"
const val ABOUT_ME = "about_me"

class WorkOrderParamsDialog : BaseBottomFragmentDialog<DialogWorkOderParamsBinding>() {
    var type: String = ""
    var title: String = ""
    var itemClick: (String) -> Unit = {}
    private val adapter =
        object : BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_single_text) {
            var itemClick: (String) -> Unit = {}
            override fun convert(holder: BaseViewHolder, item: String) {
                holder.setText(R.id.tv_title, item)
                holder.itemView.click {
                    itemClick.invoke(item)
                }
            }
        }
    private val viewModel by createViewModel<ParamsViewModel>()

    override fun DialogWorkOderParamsBinding.initView(savedInstanceState: Bundle?) {
        tvTitle.text = title
        refresh()
        rvParamsContent.adapter = adapter

    }

    override fun DialogWorkOderParamsBinding.initListener() {
        btnClose.click {
            dismiss()
        }
        adapter.itemClick = {
            itemClick.invoke(it)
            dismiss()
        }
    }

    override fun DialogWorkOderParamsBinding.initIntent(intent: Any?) {
        when (intent) {
            is ParamsViewModel.ParamsIntent -> {
                refreshFinish(intent.data != null)
                if (intent.data != null) {
                    intent.data.let {
                        when (type) {
                            WO_TYPES -> adapter.setList(it.wo_types)
                            LEVELS -> adapter.setList(it.levels)
                            STATUS -> adapter.setList(it.status)
                            TYPES -> adapter.setList(it.types)
                            CONTENTS -> adapter.setList(it.contents)
                            PROJECT_NAMES -> adapter.setList(it.project_names)
                            PROJECT_UNITS -> adapter.setList(it.project_units)
                            RECEIPT_REASON -> adapter.setList(it.receipt_reason)
                            RECEIPT_TYPE -> adapter.setList(it.receipt_type)
//                            ABOUT_ME -> adapter.setList(it.about_me)
                        }
                    }
                }
            }
        }
    }

    private fun refresh() {
        bind.llLoading.visible()
        bind.pbLoading.visible()
        bind.tvLoadingText.apply {
            text = "加载中..."
            visible()
        }
        viewModel.getParams()
    }

    private fun refreshFinish(isSuccess: Boolean) {
        if (isSuccess) {
            bind.llLoading.gone()
        } else {
            bind.pbLoading.gone()
            bind.tvLoadingText.apply {
                text = "加载失败，点击重试！"
                visible()
                click {
                    refresh()
                }
            }
        }
    }
}

class ParamsViewModel(application: Application) : BaseViewModel(application) {
    fun getParams() {
        request(isLoading = true, request = {
            workOrderParams()
        }, responseFailed = {
            toast(it.message)
            actionIntent(ParamsIntent(null))
        }, response = {
            if (it.data != null) {
                actionIntent(ParamsIntent(it.data))
            } else {
                actionIntent(ParamsIntent(null))
            }
        })
    }

    class ParamsIntent(val data: WorkOrderParamsMode?)
}

fun optionParams(fm: FragmentManager, type: String, title: String, result: (String) -> Unit) {
    WorkOrderParamsDialog().apply {
        this.title = title
        this.itemClick = result
        this.type = type
    }.show(fm, null)
}